/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.android;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Application;
import android.util.Log;

public class ComApplication extends Application {
	private static final String TAG = "ComApplication";
	
	private DefaultHttpClient httpClient;

	@Override
	public void onCreate() {
		super.onCreate();
		this.httpClient = new DefaultHttpClient();
		
		// TODO Inicializar o connection manager, que vai cuidar de guardar o estado
		// da autenticação e fazer as requisições.
	}

	public DefaultHttpClient getHttpClient() {
		return httpClient;
	}
	
	private static final String INPUT_NAME_LOGIN = "user.login";
	private static final String INPUT_NAME_PASSWORD = "user.password";
//	private static final String FORM_ACTION_LOGIN = "http://10.50.9.31:8080/GW-Application-Arquigrafia/groupware-workbench/1/userMgr/2/login";
	private static final String FORM_ACTION_LOGIN = "http://192.168.1.3:8080/GW-Application-Arquigrafia/groupware-workbench/1/userMgr/2/login";
	// TODO Obter a URL do POST a partir do XML de configuração.
	
	public void authenticateUser(String login, String password) {
		Log.d(TAG, "Autenticando o usuário '" + login + "' com password '" + password + "'.");
		DefaultHttpClient httpClient = this.getHttpClient();
		
		HttpPost httpPost = new HttpPost(FORM_ACTION_LOGIN);

		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair(INPUT_NAME_LOGIN, login));
		nameValuePairs.add(new BasicNameValuePair(INPUT_NAME_PASSWORD, password));
		
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			/*HttpResponse response =*/ httpClient.execute(httpPost);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Log.d(TAG, "Login realizado.");
		// TODO Verificar se o login foi bem sucedido.
	}
}
